<?php
/* 
 * @autor Natanael
 * Controller de Mail Control Interno
 */
class MailInterno extends Controller
{

    public function  __construct()
    {
         parent::Controller();
         $this->load->library(array('chgraficos','chcalendario','chcompany'));
         $this->load->model(array('smssetupci','mscript','mcompania','smsnotificacion',
                                   'mprocesos','headerci','detalleci','metapas','vistaproseso',
                                   'mailheaderci','maildetallesci','usuarios'));
    }

    public function index()
    {
         $this->load->view('main', array('content_page' => 'headercimail','menutop'=>'5'));
    }

    public function dataheader()
    {

          $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
	      $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'IdHether'; // Columna para el order by
          $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
	      $limit = $this->input->post('rows') ? $this->input->post('rows') : 100;
	      $start = $limit * $page - $limit;
	      if($start < 0) $start = 0;

		    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		    header("Cache-Control: no-cache, must-revalidate" );
		    header("Pragma: no-cache" );

           $contactoSearch = false;

	       $records = $this->mailheaderci->loadRegistro($start, $limit, $sortname, $sortorder);

           $totalRecords = count($records);

           $totalPages = ceil($totalRecords/$limit);

                  if ($page > $totalPages) $page = $totalPages;

	        $json_output = array(
		       'page'		=> $page,
		       'total'		=> $totalPages,
		       'records'	=> $totalRecords,
		       'rows'		=> array());

		    if($records != false) {
		  	  foreach($records as $k) {
                
		  	  	 $usuario = $this->usuarios->LoadUsuarioById($k->getUsuario());
		  	  	
		  	  	 $fecha = explode("-", $k->getFecha());
		  	  	 
				 $arr = $json_output['rows'][] = array(
				               'IdHether'     => $k->getIdHether(),
				               'Fecha'        => $fecha[1].'/'.$fecha[0].'/'.$fecha[2].' '.$k->getHora(),
				               'Tipo'         => $k->getTipo(),
				               'CantidaMail'  => $k->getCantidaMail(),
                               'Usuario'      => $usuario->getUsernameUsuario());


              }
		    }

		  $a=json_encode($json_output);
		  $a=str_replace('\\','',$a);
		  echo $a;


      }
      
      public function datadetalles()
      {
      
           $id = $this->input->post('iddetalle');
         
           $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		   $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'IdHether'; // Columna para el order by
		   $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		   $limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
		   $start = $limit * $page - $limit;
		   if($start < 0) $start = 0;
  
		    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		    header("Cache-Control: no-cache, must-revalidate" );
		    header("Pragma: no-cache" );
            
	       $records = $this->maildetallesci->loadRegistro(array("IdHether"   => $id));
		
		   $totalRecords = count($records);
		
		   $totalPages = ceil($totalRecords/$limit);
		        if ($page > $totalPages) $page = $totalPages;

		   $json_output = array(
		       'page'		=> $page,
		       'total'		=> $totalPages,
		       'records'	=> $totalRecords,
		       'rows'		=> array());
		    
		  
		   if($records != false) {
		  	    foreach($records as $k) {
		  	  	 $etapa = $this->metapas->LoadEtapaById($k->getEtapa());
		  	  	 $arr = $json_output['rows'][] = array(
				               'IdHether'         => $k->getIdHether(),
				               'Referencia'       => $k->getReferencia(),
				               'Telefono'         => $k->getTelefono(),
				               'Contacto'         => $k->getContacto(),
		  	  	               'Etapa'            => $etapa->GetNombre(),
		  	  	               'Mail'             => $k->getMail());	
			    }
		    }

		    $a=json_encode($json_output);
		    $a=str_replace('\\','',$a);
		    echo $a;
      
      }
}

